{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 为什么学习pandas\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series, DataFrame\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Series类型说明\n",
    "这个就是一种类似与一维数组的对象，它是由一组数据\n",
    "一级一组与之相关的数组标签组成（索引）。仅由一组数据可产生最简单的\n",
    "Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "4    5\n",
      "dtype: int64\n",
      "RangeIndex(start=0, stop=5, step=1)\n"
     ]
    }
   ],
   "source": [
    "obj = Series([1, 2, 3, 4, 5])\n",
    "print(obj)\n",
    "print(obj.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    a\n",
      "2    b\n",
      "3    c\n",
      "4    d\n",
      "5    e\n",
      "dtype: object\n",
      "b\n"
     ]
    }
   ],
   "source": [
    "# 自定义索引\n",
    "obj = Series(['a','b','c','d','e'], index=[1,2,3,4,5,])\n",
    "print(obj)\n",
    "print(obj[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1000\n",
      "b    2000\n",
      "c    3000\n",
      "dtype: int64\n",
      "a    1000\n",
      "c    3000\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 我们也可以吧Series当做字典来使用\n",
    "data = {'a':1000, 'b':2000, 'c':3000}\n",
    "obj = Series(data)\n",
    "print(obj)\n",
    "keys = ['a', 'c']\n",
    "obj_1 = Series(data, index=keys)\n",
    "print(obj_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a       NaN\n",
      "b    1000.0\n",
      "c    2000.0\n",
      "dtype: float64\n",
      "a     True\n",
      "b    False\n",
      "c    False\n",
      "dtype: bool\n",
      "a    False\n",
      "b     True\n",
      "c     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# 缺失数据的处理\n",
    "data = {'a':None, 'b':1000, 'c':2000}\n",
    "obj = Series(data)\n",
    "print(obj)\n",
    "print(pd.isnull(obj))\n",
    "print(pd.notnull(obj))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xinming\n",
      "LiLei         NaN\n",
      "HanMeimei    25.0\n",
      "Tony          NaN\n",
      "Jack         50.0\n",
      "Name: NameandAge, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "data = {'LiLei':None, 'HanMeimei':25, 'Tony':None,'Jack':50}\n",
    "obj = Series(data)\n",
    "obj.name = 'NameandAge'\n",
    "obj.index.name = 'xinming'\n",
    "print(obj)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataframe类型\n",
    "Dataframe是一个表格型的数据结构，它含有一组有序\n",
    "的列。每列可以是不同值的类型、数值、字符串、布尔值都可以\n",
    "##### Dataframe本身有行索引，也有列索引\n",
    "##### Dataframe也可以理解成Series组成的一个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  60年代 70年代 80年代\n",
      "0   狗子   卫国   李雷\n",
      "1   嘎子   建国  韩梅梅\n",
      "2   秀儿   爱国   张伟\n",
      "0    卫国\n",
      "1    建国\n",
      "2    爱国\n",
      "Name: 70年代, dtype: object\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    '60年代':['狗子','嘎子','秀儿'],\n",
    "    '70年代':['卫国','建国','爱国'],\n",
    "    '80年代':['李雷','韩梅梅','张伟']\n",
    "}\n",
    "frame_data = DataFrame(data)\n",
    "print(frame_data)\n",
    "print(frame_data['70年代'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19',\n",
      "               '2020-06-20', '2020-06-21'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "dates = pd.date_range('20200616', periods=6)\n",
    "print(dates)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2020-06-16  0.357414  0.413115  0.835634  0.558702\n",
      "2020-06-17  0.878287  0.832637  0.881887  0.304992\n",
      "2020-06-18  0.254230  0.386165  0.428383  0.965134\n",
      "2020-06-19  0.157503  0.499387  0.527166  0.329053\n",
      "2020-06-20  0.630095  0.202250  0.594205  0.599915\n",
      "2020-06-21  0.630977  0.948468  0.164462  0.976351\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(6,4), index=dates, columns=list('ABCD'))\n",
    "print(df)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2020-06-16  0.357414  0.413115  0.835634  0.558702\n",
      "2020-06-17  0.878287  0.832637  0.881887  0.304992\n",
      "2020-06-18  0.254230  0.386165  0.428383  0.965134\n",
      "\n",
      "                    A         B\n",
      "2020-06-16  0.357414  0.413115\n",
      "2020-06-17  0.878287  0.832637\n",
      "2020-06-18  0.254230  0.386165\n",
      "\n",
      " 0.3574141218924294\n",
      "\n",
      "                    A         B         C         D\n",
      "2020-06-16  0.357414  0.413115  0.835634  0.558702\n",
      "2020-06-17  0.878287  0.832637  0.881887  0.304992\n",
      "\n",
      "                    A         B         C         D\n",
      "2020-06-19  0.157503  0.499387  0.527166  0.329053\n",
      "2020-06-20  0.630095  0.202250  0.594205  0.599915\n",
      "2020-06-21  0.630977  0.948468  0.164462  0.976351\n"
     ]
    }
   ],
   "source": [
    "print(df['20200616':'20200618'])\n",
    "print('\\n', df.loc['20200616':'20200618', ['A', 'B']])\n",
    "print('\\n', df.at[dates[0], 'A'])\n",
    "print('\\n', df.head(2))\n",
    "print('\\n', df.tail(3))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame构造函数能够接收那些数据类型？？\n",
    "1、二维numpy arrary  <br>\n",
    "2、由数组、列表或者是元祖组成的字典 <br>\n",
    "3、由Series组成的字典  <br>\n",
    "4、由字典组成的字典 <br>\n",
    "5、字典或Series的列表<br>\n",
    "6、由列表或元祖组成的列表<br>\n",
    "7、另一个DataFrame<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### pandas重新索引\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    3.4\n",
      "b    2.1\n",
      "c   -1.7\n",
      "dtype: float64\n",
      "a    3.4\n",
      "b    2.1\n",
      "c   -1.7\n",
      "d    NaN\n",
      "e    NaN\n",
      "dtype: float64\n",
      "\n",
      " a    3.4\n",
      "b    2.1\n",
      "c   -1.7\n",
      "d    1.0\n",
      "e    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "obj = Series([3.4, 2.1, -1.7], index=list('abc'))\n",
    "print(obj)\n",
    "job_1 = obj.reindex(['a','b','c','d','e'])\n",
    "print(job_1)\n",
    "print('\\n', obj.reindex(['a','b','c','d','e'],fill_value=1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    3.4\n",
      "2    2.1\n",
      "4   -1.7\n",
      "dtype: float64\n",
      "0    3.4\n",
      "1    3.4\n",
      "2    2.1\n",
      "3    2.1\n",
      "4   -1.7\n",
      "5   -1.7\n",
      "dtype: float64\n",
      "0    3.4\n",
      "1    2.1\n",
      "2    2.1\n",
      "3   -1.7\n",
      "4   -1.7\n",
      "5    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "## 前向值填充\n",
    "obj = Series([3.4, 2.1, -1.7], index=[0,2,4])\n",
    "print(obj)\n",
    "obj_1 = obj.reindex(np.arange(6), method='ffill') # \n",
    "print(obj_1)\n",
    "obj_2 = obj.reindex(np.arange(6), method='bfill')   # 后向值填充\n",
    "print(obj_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 算术运算和数据对齐"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Dataframe和Series之间的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   b   d   e\n",
      "0  0   1   2\n",
      "1  3   4   5\n",
      "2  6   7   8\n",
      "3  9  10  11\n",
      "b    0\n",
      "d    1\n",
      "e    2\n",
      "Name: 0, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "frame = DataFrame(np.arange(12).reshape((4,3)), columns=['b','d','e'], index=list(np.arange(4)))\n",
    "print(frame)\n",
    "series = frame.loc[0]        # 选取frame中为1的一行数据\n",
    "print(series)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   b  d  e\n",
       "0  0  0  0\n",
       "1  3  3  3\n",
       "2  6  6  6\n",
       "3  9  9  9"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame - series  # 一直向下广播相减"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序\n",
    "根据条件对数据集进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d    0\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "obj = Series(range(4), index=['d', 'a', 'b', 'c'])\n",
    "print(obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(a    1\n",
       " b    2\n",
       " c    3\n",
       " d    0\n",
       " dtype: int64,\n",
       " d    0\n",
       " a    1\n",
       " b    2\n",
       " c    3\n",
       " dtype: int64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.sort_index(), obj.sort_values()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a  c  d  b\n",
      "two  0  1  2  3\n",
      "one  4  5  6  7\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     a  c  d  b\n",
       "one  4  5  6  7\n",
       "two  0  1  2  3"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 针对DataFrame指定轴进行排序\n",
    "obj = DataFrame(np.arange(8).reshape((2,4)), index=['two', 'one'], columns=['a', 'c', 'd', 'b'])\n",
    "print(obj)\n",
    "obj.sort_index(axis=0)  # 指定轴进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   b  a\n",
      "0  1  3\n",
      "1  4  5\n",
      "2  0  8\n",
      "3  6  1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   b  a\n",
       "2  0  8\n",
       "0  1  3\n",
       "1  4  5\n",
       "3  6  1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = DataFrame({'b':[1, 4, 0, 6], 'a':[3, 5, 8 ,1]})\n",
    "print(frame)\n",
    "frame.sort_values(by='b')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 层次化索引\n",
    "#### 层次化索引是pandas的一项重要的功能， <br>他能够让你在一个周上拥有多个索引级别<br>另一种说法是他可以以低纬度形式处理高纬度数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(a  0   -0.184402\n    1    0.085480\n    2    1.723195\n b  3   -0.096874\n    4    0.911083\n    5    0.807061\n c  6   -1.938723\n    7   -0.519010\n    8    2.700634\n d  9    0.067934\n dtype: float64,\n MultiIndex([('a', 0),\n             ('a', 1),\n             ('a', 2),\n             ('b', 3),\n             ('b', 4),\n             ('b', 5),\n             ('c', 6),\n             ('c', 7),\n             ('c', 8),\n             ('d', 9)],\n            ))"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = Series(np.random.randn(10), index=[['a','a','a','b','b','b','c','c','c','d'],np.arange(10)])\n",
    "data, data.index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3   -0.096874\n",
      "4    0.911083\n",
      "5    0.807061\n",
      "dtype: float64\n",
      "\n",
      " b  3   -0.096874\n",
      "   4    0.911083\n",
      "   5    0.807061\n",
      "c  6   -1.938723\n",
      "   7   -0.519010\n",
      "   8    2.700634\n",
      "d  9    0.067934\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(data['b'])\n",
    "print('\\n', data['b':'d'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1.723195\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "## 内层索引\n",
    "print(data[:,2]) #  第一层选取所有，第二层选取2\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  0   -0.184402\n",
      "   1    0.085480\n",
      "   2    1.723195\n",
      "b  3   -0.096874\n",
      "   4    0.911083\n",
      "   5    0.807061\n",
      "c  6   -1.938723\n",
      "   7   -0.519010\n",
      "   8    2.700634\n",
      "d  9    0.067934\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "data.unstack()  #  生成一个新的DataFrame方法\n",
    "print(data.unstack().stack())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Dataframe，每条轴都可以有分层索引，<br>各层也都是可以有名字的\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "        Black    Yellow      Bule\n        Green       Red     Green\na 1  0.123289  0.430228  0.566080\n  2  0.980921  0.857866  0.801727\nb 1  0.303045  0.125592  0.196975\n  2  0.592765  0.201217  0.117592",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>Black</th>\n      <th>Yellow</th>\n      <th>Bule</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0.123289</td>\n      <td>0.430228</td>\n      <td>0.566080</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.980921</td>\n      <td>0.857866</td>\n      <td>0.801727</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>0.303045</td>\n      <td>0.125592</td>\n      <td>0.196975</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.592765</td>\n      <td>0.201217</td>\n      <td>0.117592</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_data = DataFrame(np.random.rand(4,3),\n",
    "                       index=[['a','a','b','b'],[1, 2, 1, 2]],\n",
    "                       columns=[['Black','Yellow','Bule'],['Green','Red','Green']])\n",
    "frame_data"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "# 行索引的名字命名\n",
    "frame_data.index.names=['key1','key2']\n",
    "frame_data"
   ],
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "              Black    Yellow      Bule\n              Green       Red     Green\nkey1 key2                              \na    1     0.123289  0.430228  0.566080\n     2     0.980921  0.857866  0.801727\nb    1     0.303045  0.125592  0.196975\n     2     0.592765  0.201217  0.117592",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>Black</th>\n      <th>Yellow</th>\n      <th>Bule</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0.123289</td>\n      <td>0.430228</td>\n      <td>0.566080</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.980921</td>\n      <td>0.857866</td>\n      <td>0.801727</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>0.303045</td>\n      <td>0.125592</td>\n      <td>0.196975</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.592765</td>\n      <td>0.201217</td>\n      <td>0.117592</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "color1        Black    Yellow      Bule\ncolor2        Green       Red     Green\nkey1 key2                              \na    1     0.123289  0.430228  0.566080\n     2     0.980921  0.857866  0.801727\nb    1     0.303045  0.125592  0.196975\n     2     0.592765  0.201217  0.117592",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>color1</th>\n      <th>Black</th>\n      <th>Yellow</th>\n      <th>Bule</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color2</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0.123289</td>\n      <td>0.430228</td>\n      <td>0.566080</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.980921</td>\n      <td>0.857866</td>\n      <td>0.801727</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>0.303045</td>\n      <td>0.125592</td>\n      <td>0.196975</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.592765</td>\n      <td>0.201217</td>\n      <td>0.117592</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列索引名字设置\n",
    "frame_data.columns.names=['color1','color2']\n",
    "frame_data"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "frame_data = DataFrame(np.random.rand(4,3),\n",
    "                       index=[['a','a','b','b'],[1, 2, 1, 2]],\n",
    "                       columns=[['Black','Yellow','Black'],['Green','Red','Green']])\n",
    "frame_data.index.names=['key1','key2']\n",
    "frame_data.columns.names=['color1','color2']\n",
    "frame_data"
   ],
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "color1        Black    Yellow     Black\ncolor2        Green       Red     Green\nkey1 key2                              \na    1     0.913085  0.844804  0.599248\n     2     0.088922  0.572266  0.519601\nb    1     0.954481  0.479709  0.230571\n     2     0.881690  0.318254  0.773414",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>color1</th>\n      <th>Black</th>\n      <th>Yellow</th>\n      <th>Black</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color2</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0.913085</td>\n      <td>0.844804</td>\n      <td>0.599248</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.088922</td>\n      <td>0.572266</td>\n      <td>0.519601</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>0.954481</td>\n      <td>0.479709</td>\n      <td>0.230571</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.881690</td>\n      <td>0.318254</td>\n      <td>0.773414</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "        Green     Green\na 1  0.597469  0.844998\n  2  0.753469  0.937345\nb 1  0.116394  0.573487\n  2  0.386198  0.124057",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Green</th>\n      <th>Green</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0.597469</td>\n      <td>0.844998</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.753469</td>\n      <td>0.937345</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>0.116394</td>\n      <td>0.573487</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.386198</td>\n      <td>0.124057</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Dataframe类型层次化索引的访问\n",
    "frame_data['Black']"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "# 行和列同时进行访问筛选\n",
    "frame_data.loc['a',['Black']]"
   ],
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "      Black          \n      Green     Green\n1  0.597469  0.844998\n2  0.753469  0.937345",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"2\" halign=\"left\">Black</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Green</th>\n      <th>Green</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>0.597469</td>\n      <td>0.844998</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.753469</td>\n      <td>0.937345</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "color1     Black    Yellow     Black\ncolor2     Green       Red     Green\nkey2                                \n1       1.867565  1.324514  0.829819\n2       0.970612  0.890520  1.293015",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th>color1</th>\n      <th>Black</th>\n      <th>Yellow</th>\n      <th>Black</th>\n    </tr>\n    <tr>\n      <th>color2</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1.867565</td>\n      <td>1.324514</td>\n      <td>0.829819</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.970612</td>\n      <td>0.890520</td>\n      <td>1.293015</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 根据级别汇总统计\n",
    "frame_data.sum(level='key2')"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "frame_data.sum(level='color2',axis=1)\n"
   ],
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "color2        Green       Red\nkey1 key2                    \na    1     1.512333  0.844804\n     2     0.608523  0.572266\nb    1     1.185052  0.479709\n     2     1.655104  0.318254",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>color2</th>\n      <th>Green</th>\n      <th>Red</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>1.512333</td>\n      <td>0.844804</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.608523</td>\n      <td>0.572266</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>1.185052</td>\n      <td>0.479709</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1.655104</td>\n      <td>0.318254</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## pandas文本格式数据处理\n",
    "1、read_csv:从文本型对象中加载带分隔符的数据，默认分隔符为逗号<br>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}